﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Pottery.Frontend.Models;
using Pottery.Frontend.ViewModels;
using Softserve.Pottery.Store;
using Softserve.Pottery.Store.Domains;
using ServiceProvider = Softserve.Pottery.ServiceLocator.ServiceLocator;

namespace Pottery.Frontend.Controllers
{
    public class CartController : Controller
    {
        public ActionResult Index()
        {
            var model = new CartViewModel { Cart = GetCart() };
            return View(model);
        }

        /// <summary>
        /// Add new item to a cart
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="returnUrl"></param>
        /// <returns>Object of ActionResult type</returns>
        public ActionResult AddToCart(int productId, string returnUrl)
        {
            //Product product = m_StoreModule.GetProductById(productId);
            Product product = (from p in Mocks.products
                               where p.Id == productId
                               select p)
                               .FirstOrDefault();

            if (product != null)
            {
                GetCart().AddItem(product);
            }

            return RedirectToAction("ReturnToUrl", "Product", new { returnUrl });
        }

        /// <summary>
        /// Remove item from a cart
        /// </summary>
        /// <param name="productId"></param>
        /// <param name="returnUrl"></param>
        /// <returns>Object of ActionResult type</returns>
        public ActionResult RemoveFromCart(int productId, string returnUrl)
        {
            //Product product = m_StoreModule.GetProductById(productId);
            Product product = (from p in Mocks.products
                               where p.Id == productId
                               select p)
                              .FirstOrDefault();

            if (product != null)
            {
                GetCart().RemoveItem(product);
            }

            return RedirectToAction("Index");
        }

        public ActionResult EditLineInCart(int productId, int quantity)
        {
            //Product product = m_StoreModule.GetProductById(productId);
            Product product = (from p in Mocks.products
                               where p.Id == productId
                               select p)
                              .FirstOrDefault();

            if (product != null)
            {
                GetCart().EditItem(productId, quantity);
            }

            return RedirectToAction("Index");
        }

        private Cart GetCart()
        {
            Cart cart = (Cart)Session["Cart"];

            if (cart == null)
            {
                cart = new Cart();
                Session["Cart"] = cart;
            }

            return cart;
        }

        private IStoreScopeContract m_StoreModule = ServiceLocator.GetService(typeof(IStoreScopeContract)) as IStoreScopeContract;
        private static readonly ServiceProvider ServiceLocator = ServiceProvider.CreateInstance;

    }
}
